<?php

namespace App\Http\Controllers;

use App\Jobs\ArenacontestNotice;
use App\Models\Arenacontest;
use App\Models\ArenacontestUser;
use App\Models\WechatUser;
use Carbon\Carbon;
use Illuminate\Http\Request;

class ArenacontestController extends Controller
{
    function list() {
        $city = request()->input('city', 0);
        $city = rtrim($city, '0');
        if (strlen($city) % 2 === 1) {
            $city = $city . '0';
        }
        $city .= '%';

        $pn = request()->input('pn', config('api.page_num'));
        $list = Arenacontest::where([ ])
            ->select('id', 'title', 'game_datetime', 'signup_datetime', 'address', 'remark', 'status', 'numcount','organizer_name')
            ->orderBy('game_datetime', 'desc')
            ->paginate($pn);
        return response()->json(['recode' => 1,
            'data' => ['last_page' => $list->lastPage()
                , 'page' => $list->currentPage()
                , 'list' => $list->items()]]);

    }

    public function info()
    {
        $id = request()->input('id', 0);
        $userId = request()->input('user_id', 0);

        $info = Arenacontest::find($id);
        if (!$info) {
            return response()->json(
                [
                    'recode' => 0,
                    'msg' => '未找到详情',
                ]
            );
        }
        //更新观看次数
        $info->view += 1;
        $info->save();

        //查询用户是否已经报名
        $ArenacontestUser = ArenacontestUser::where([
            ['Arenacontest_id', $id],
            ['user_id', $userId],
            ['status', 10],
        ])
            ->first();
        $isSignup = 0;
        if ($ArenacontestUser) {
            $isSignup = 1;
        }
        //查询报名人员
        $signupList = ArenacontestUser::where([
            ['Arenacontest_id', $id],
            ['status', 10],
        ])
            ->select('id', 'user_id')
            ->with('user:id,name,head_img')
            ->limit(5)
            ->get();
        $signupCount = ArenacontestUser::where([
            ['Arenacontest_id', $id],
            ['status', 10],
        ])->count();

        //查询擂主
        $organizerInfo = WechatUser::find($info->organizer);

        if($ArenacontestUser){
            $pay_status=$ArenacontestUser->pay_status;
        }else{
            $pay_status='';
        }
        return response()->json(
            [
                'recode' => 1,
                'info' => $info,
                'signup' => $isSignup,
                'signup_list' => $signupList,
                'signup_count' => $signupCount,
                'pay_status'=>$pay_status,
                'organizerInfo'=>$organizerInfo
            ]
        );

    }

    public function startGame()
    {
        $userId = request()->input('user_id', 0);
        $gameDatetime = request()->input('game_datetime');
        $endGameDatetime = request()->input('end_game_datetime');
        $signupDatetime = request()->input('signup_datetime');
        $title = request()->input('title', '');
        $address = request()->input('address', '');
        $signupPrice = request()->input('signup_price', '0');
        $prize = request()->input('prize', '');
        $remark = request()->input('remark', '');

        $userInfo = WechatUser::find($userId);

        if ($userId == 0 || !$userInfo) {
            return response()->json(['recode' => 0, 'msg' => '组织者为空']);
        }
        if ($gameDatetime < $signupDatetime) {
            return response()->json(['recode' => 0, 'msg' => '报名截止时间晚于比赛时间']);
        }
        if ($gameDatetime > $endGameDatetime) {
            return response()->json(['recode' => 0, 'msg' => '比赛开始时间晚于结束时间']);
        }
        if ($title == '') {
            return response()->json(['recode' => 0, 'msg' => '标题不能为空']);
        }
        if ($address == '') {
            return response()->json(['recode' => 0, 'msg' => '地点不能为空']);
        }

        $Arenacontest = new Arenacontest;
        $Arenacontest->city = $userInfo->city;
        $Arenacontest->organizer = $userId;
        $Arenacontest->title = $title;
        $Arenacontest->game_datetime = $gameDatetime;
        $Arenacontest->end_game_datetime = $endGameDatetime;
        $Arenacontest->address = $address;
        $Arenacontest->signup_datetime = $signupDatetime;
        $Arenacontest->signup_price = $signupPrice;
        $Arenacontest->prize = $prize;
        $Arenacontest->remark = $remark;
        $Arenacontest->save();
        //发送通知
        $this->dispatch((new ArenacontestNotice($Arenacontest)));

        return response()->json(['recode' => 1, 'id' => $Arenacontest->id, 'msg' => '保存成功']);

    }

    public function getArenacontestByUser()
    {
        $pn = request()->input('pn', config('api.page_num'));
        $userId = request()->input('user_id');

        $list = Arenacontest::where([ ])
            ->select('arenacontest.id', 'arenacontest.title', 'arenacontest.game_datetime', 'arenacontest.signup_datetime', 'arenacontest.address', 'arenacontest.remark', 'arenacontest.status', 'arenacontest.numcount','arenacontest.organizer_name','arenacontest_users.pay_status','arenacontest_users.ordersn','arenacontest_users.transid')
            ->leftJoin('arenacontest_users', 'arenacontest.id', '=', 'arenacontest_users.arenacontest_id')
            ->where('user_id', $userId)
            ->orderBy('arenacontest.game_datetime', 'desc')
            ->paginate($pn);
        return response()->json(['recode' => 1,
            'data' => ['last_page' => $list->lastPage()
                , 'page' => $list->currentPage()
                , 'list' => $list->items()]]);
    }

    public function getWechatUserList(Request $request)
    {
        $q = $request->get('q');
        if ($q!=''){
            return WechatUser::where('name', 'like', "%$q%")->paginate(null, ['id', 'name as text']);
        }else{
            return WechatUser::get()->paginate(null, ['id', 'name as text']);
        }
    }
}
